package com.wangsd.system.controller;

import com.gitee.fastmybatis.core.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.controller.AjaxResult;
import com.wangsd.common.controller.BaseController;
import com.wangsd.common.jwt.LoginUser;
import com.wangsd.common.jwt.TokenService;
import com.wangsd.common.utils.ServletUtils;
import com.wangsd.system.entity.Coupon;
import com.wangsd.system.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券Controller
 *
 * @author wsd
 * @date 2021-06-18
 */
@Api(tags = "优惠券管理")
@RestController
@RequestMapping("/system/coupon")
public class CouponController extends BaseController {
    @Autowired
    private CouponService couponService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "查询优惠券列表")
    @GetMapping("/list")
    public AjaxResult<PageInfo<Coupon>> list(Coupon coupon) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        Page page = PageHelper.startPage(ServletUtils.getParameterToInt("pageNum"), ServletUtils.getParameterToInt("pageSize"));
        List<Coupon> list = couponService.selectCouponList(coupon);
        return AjaxResult.success(getPageInfo(page, list));
    }


    @ApiOperation(value = "获取优惠券详细信息")
    @GetMapping("/{id}")
    public AjaxResult<Coupon> getInfo(@PathVariable("id") Long id) {
        Coupon coupon = couponService.selectCouponById(id);
        return AjaxResult.success(coupon);
    }

    @ApiOperation(value = "新增/修改优惠券")
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody Coupon coupon) {
        if (coupon.getId() == null){
            couponService.insertCoupon(coupon);
        } else {
            couponService.updateCoupon(coupon);
        }
        return AjaxResult.success("success");
    }


    @ApiOperation(value = "删除优惠券")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        couponService.deleteCouponById(id);
        return AjaxResult.success("success");
    }

    @ApiOperation(value = "启用 禁用")
    @PutMapping("/status/{id}")
    public AjaxResult status(@PathVariable Long id) {
        couponService.changeStatus(id);
        return AjaxResult.success("success");
    }
}